#include "stdafx.h"
#include "util/util.h"
#include "command.h"
#include <shellapi.h>

namespace shared
{
	namespace command {
		std::map<std::wstring, std::wstring> g_commond_map;

		void ParseCommand(const std::wstring& sCmd) {
			wprintf(L"ParseCommand %s\n", sCmd.c_str());
			std::vector<std::wstring> vecKey;
			vecKey.push_back(L"/");
			std::vector<std::wstring> vecCmd = shared::stringhelp::SplitStringEx<std::wstring>(sCmd, vecKey, false);
			for (auto it = vecCmd.begin(); it != vecCmd.end(); it++)
			{
				std::vector<std::wstring> key;
				key.push_back(L" ");
				std::vector<std::wstring> vecObject = shared::stringhelp::SplitStringEx<std::wstring>(*it, key, false);
				assert(vecObject.size() == 2);
				if (vecObject.size() == 2) {
					std::wstring sKey = *vecObject.begin();
					std::wstring sValue = *vecObject.rbegin();
					nbase::StringTrim(sKey);
					nbase::StringTrim(sValue);
					assert(!sKey.empty() && !sValue.empty());
					g_commond_map[sKey] = sValue;
				}	
			}
			printf("ParseCommand g_commond_map size=%d\n", g_commond_map.size());
		}

		bool AppStart(const std::wstring& sApp, const std::wstring& sCmd) {
			HINSTANCE hInst = ::ShellExecuteW(NULL, L"open", sApp.c_str(), sCmd.c_str(), NULL, SW_SHOWNORMAL);
			return (int)hInst > 32;
		}
		bool RestartApp(const std::wstring& sCmd) {
			wchar_t app[1024] = { 0 };
			GetModuleFileName(NULL, app, 1024);
			HINSTANCE hInst = ::ShellExecuteW(NULL, L"open", app, sCmd.c_str(), NULL, SW_SHOWNORMAL);
			return (int)hInst > 32;
		}
		std::wstring Get(const std::wstring& sKey) {
			std::map<std::wstring, std::wstring>::const_iterator i = g_commond_map.find(sKey);
			if (i == g_commond_map.end())
				return L"";
			else
				return i->second;
		}
		void Set(const std::wstring& sKey, const std::wstring& sValue) {
			g_commond_map[sKey] = sValue;
		}
		void Erase(const std::wstring& sKey) {
			g_commond_map.erase(sKey);
		}

	}
}



